package controllers

import (
	"blog/models"
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/cache"
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/utils/pagination"
	"strconv"
	"time"
)

//  ArticleController operations for Article
type ArticleController struct {
	beego.Controller
}

func (c *ArticleController) Index() {
	var fields []string
	var sortby []string
	var order []string
	var query = make(map[string]string)
	var limit int64 = 10
	var offset int64
	Keywords := c.GetString("Keywords")
	CatId := c.GetString("CatId")

	categories, err := models.GetAllArticle_category(query, fields, sortby, order, offset, limit)
	if err != nil {
		logs.Error(err)
		c.Abort("404")
	} else {
		c.Data["Categories"] = categories
	}
	limit = 3
	sortby = []string{"Count"}
	order = []string{"desc"}
	recent, err := models.GetAllArticle(query, fields, sortby, order, offset, limit)
	if err != nil {
		logs.Error(err)
		c.Abort("404")
	} else {
		c.Data["Recent"] = recent
	}
	page, err1 := c.GetInt("p")
	condition := make(map[string]string)
	condition["Keywords"] = Keywords
	condition["CatId"] = CatId
	if err1 != nil {
		page = 1
	}
	offset = 4
	countArticle := models.GetArticleCount(condition)

	paginator := pagination.SetPaginator(c.Ctx, int(offset), countArticle)
	_, _, articles := models.ListArticles(condition, page, int(offset))
	//fmt.Print(articles)

	c.Data["Paginator"] = paginator
	c.Data["Articles"] = articles

	c.Data["Title"] = "首页"
	c.Layout = "blog/blog_layout.tpl"
	c.TplName = "blog/blog_content.tpl"
	c.LayoutSections = make(map[string]string)
	c.LayoutSections["Head"] = "common/head.tpl"
	c.LayoutSections["Menu"] = "common/menu.tpl"
	c.LayoutSections["Footer"] = "common/footer.tpl"
	c.LayoutSections["Script"] = "common/script.tpl"
	c.LayoutSections["Aside"] = "common/aside.tpl"
	c.LayoutSections["OtherScript"] = "blog/blog_script.tpl"
	c.LayoutSections["OtherHead"] = "blog/blog_head.tpl"
	c.LayoutSections["BeforeContent"] = "blog/before_content.tpl"
}

func (c *ArticleController) GetOne() {
	idStr := c.Ctx.Input.Param(":id")
	id, _ := strconv.ParseInt(idStr, 0, 64)
	v, err := models.GetArticleById(id)
	if err != nil {
		c.Abort("404")
	} else {
		var fields []string
		var sortby []string
		var order []string
		var query = make(map[string]string)
		var limit int64 = 10
		var offset int64

		categories, err := models.GetAllArticle_category(query, fields, sortby, order, offset, limit)
		if err != nil {
			c.Abort("404")
		} else {
			c.Data["Categories"] = categories
		}
		limit = 3
		recent, err := models.GetAllArticle(query, fields, sortby, order, offset, limit)
		if err != nil {
			c.Abort("404")
		} else {
			c.Data["Recent"] = recent
		}
		nowTime := time.Now().Local().Unix()
		todayEnd := time.Now().Format("2006-01-02") + " 23:59:59"
		EndTime, _ := time.ParseInLocation("2006-01-02 15:04:05", todayEnd, time.Local)
		endTime := EndTime.Local().Unix()
		remainTime := endTime - nowTime

		ip := c.Ctx.Input.IP()
		bm, err := cache.NewCache("file", `{}`)

		cacheKey := string(ip) + "_" + string(id)
		if cacheValue := bm.Get(cacheKey); cacheValue != "" {

			logs.Info("今天已经浏览过啦")

			fmt.Println(cacheValue)

		} else {

			num, err := models.IncreaseCountById(id)
			fmt.Println(num)
			fmt.Println(err)
			if err == nil {
				bm.Put(cacheKey, 1, time.Duration(remainTime)*time.Second)
			}
		}

		c.Data["Article"] = v
		c.Data["Title"] = v.Title
		c.Layout = "single/single_layout.tpl"
		c.TplName = "single/single_content.tpl"
		c.LayoutSections = make(map[string]string)
		c.LayoutSections["Head"] = "common/head.tpl"
		c.LayoutSections["Menu"] = "common/menu.tpl"
		c.LayoutSections["Footer"] = "common/footer.tpl"
		c.LayoutSections["Script"] = "common/script.tpl"
		c.LayoutSections["Aside"] = "common/aside.tpl"
	}
}
